<template>
  <div class="todo-footer" v-show="todos.length">
      <label>
      <!-- <input type="checkbox" :checked="all" @change="operatorAll"/> -->
      <input type="checkbox" v-model="all"/>
      </label>
      <span>
      <span>已完成{{ doneTotal }}</span> / 全部{{ todos.length }}
      </span>
      <button class="btn btn-danger" @click="clear">清除已完成任务</button>
  </div>
</template>

<script>
export default {
    name: 'TodoFooter',
    props: ['todos','clearDone'],
    computed: {
        doneTotal() {
            // return this.todos.filter(item => item.completed).length
            // reduce 用法
            return this.todos.reduce((pre, item) => pre + (item.completed ? 1 : 0), 0)
        },
        // all() {
        //     return this.todos.length >0 && this.todos.every(item => item.completed)
        // },
        all: {
          get(){
            return this.todos.length >0 && this.todos.every(item => item.completed)
          },
          set(val){
            this.todos.forEach(item => item.completed = val)
          }
        }
    },
    methods: {
        clear() {
            this.clearDone()
        },
        operatorAll(e) {
            this.todos.forEach(item => item.completed = e.target.checked)
        }
    }
}
</script>

<style scoped>
  /*footer*/
  .todo-footer {
    height: 40px;
    line-height: 40px;
    padding-left: 6px;
    margin-top: 5px;
    }

    .todo-footer label {
    display: inline-block;
    margin-right: 20px;
    cursor: pointer;
    }

    .todo-footer label input {
    position: relative;
    top: -1px;
    vertical-align: middle;
    margin-right: 5px;
    }

    .todo-footer button {
    float: right;
    margin-top: 5px;
    }
</style>